package com.spring.jwt.token.config;

import com.spring.jwt.token.properties.PermitAllUrlProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author suncheng
 * @version 1.0
 * @description: TODO
 * @date 2025/5/30 13:52
 */
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
@EnableConfigurationProperties(PermitAllUrlProperties.class)
public class SecurityConfig {

    private final PermitAllUrlProperties permitAllUrlProperties;

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeHttpRequests(authorize -> authorize
                        .requestMatchers(permitAllUrlProperties.getUrls().toArray(new String[0])).permitAll()
                        .anyRequest().authenticated()
                )
                .formLogin(); // 可以使用内置登录页做验证

        return http.build();
    }
}
